package com.springboot.study.tests.threadlocal;

/**
 * @Author: guodong
 * @Date: 2020/11/27 11:28
 * @Version: 1.0
 * @Description:
 */
public class Service {

//    private static ThreadLocal<Integer> requestIdThreadLocal = new ThreadLocal<>();

    private static InheritableThreadLocal<Integer> requestIdThreadLocal = new InheritableThreadLocal<>();


    public static void main(String[] args) {
        Integer reqId = new Integer(5);
        Service a = new Service();
        a.setRequestId(reqId);
    }

    public void setRequestId(Integer requestId) {
        requestIdThreadLocal.set(requestId);
        doBussiness();
    }


    public void doBussiness() {
        System.out.println("父线程 主线程----首先打印requestId:" + requestIdThreadLocal.get());

        (new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("子线程---启动");
                System.out.println("在子线程中访问requestId:" + requestIdThreadLocal.get());
            }
        })).start();

    }


}
